package com.wise.wing.views;

import java.util.List;

import com.wise.wing.R;
import com.wise.wing.bean.TCity;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;



@SuppressLint("NewApi")
public class CityPickerDialog extends Dialog implements  android.view.View.OnClickListener, AnimatorListener {

	private static int theme = android.R.style.Theme_Translucent_NoTitleBar;
	private Context context;
	private View contentView;
	private LinearLayout ll_parent;
	private RelativeLayout ll_container;
	private Button btn_ok, btn_cancel;
	private CityPicker city_picker;
	private TextView tv_loading;

	private AnimatorSet showSet;
	private AnimatorSet hideSet;

	private onAddressSelectedListener listener;

	public CityPickerDialog(Context context) {
		super(context, theme);
		this.context = context;
		init();
	}

	public CityPickerDialog(Context context, String message) {
		super(context, theme);
		this.context = context;
		init();
	}

	public CityPickerDialog(Context context, int message) {
		super(context, theme);
		this.context = context;
		init();
	}

	public void setonAddressSelectedListener(onAddressSelectedListener listener) {
		this.listener = listener;
	}

	public void setData(List<TCity> cities) {
		city_picker.setData(cities);
	}
	
	public void onLoadingCompleted(){
		tv_loading.setVisibility(View.GONE);
	}

	@SuppressLint("InflateParams")
	private void init() {
		contentView = LayoutInflater.from(context).inflate(R.layout.layout_address_picker_dialog, null);
		setContentView(contentView);
		setCanceledOnTouchOutside(true);

		btn_ok = (Button) contentView.findViewById(R.id.btn_ok);
		btn_cancel = (Button) contentView.findViewById(R.id.btn_cancel);
		city_picker = (CityPicker) contentView.findViewById(R.id.city_picker);
		ll_parent = (LinearLayout) contentView.findViewById(R.id.ll_parent);
		ll_container = (RelativeLayout) contentView.findViewById(R.id.ll_container);
		tv_loading = (TextView) contentView.findViewById(R.id.tv_loading);

		btn_ok.setOnClickListener(this);
		btn_cancel.setOnClickListener(this);
		ll_container.setOnClickListener(this);

		
		initAnimator();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_ok:
			if (listener != null){
				listener.onAddressSelected(city_picker.getAreaId(), city_picker.getSelectedAddress());}
			dismiss();
			break;

		case R.id.btn_cancel:
			dismiss();
			break;
			
		case R.id.ll_container:
			dismiss();
			break;
		}
	}

	public interface onAddressSelectedListener {
		void onAddressSelected(int areaId, String address);
	}

	private void initAnimator() {
		showSet = new AnimatorSet();
		hideSet = new AnimatorSet();
		ObjectAnimator showTranslationY = ObjectAnimator.ofFloat(ll_parent, "translationY", 300f, 0f);
		ObjectAnimator showAlpha = ObjectAnimator.ofFloat(ll_parent, "alpha", 0f, 1f);
		ObjectAnimator showAlphaC = ObjectAnimator.ofFloat(ll_container, "alpha", 0f, 1f);

		showSet.play(showTranslationY).with(showAlpha).with(showAlphaC);
		showSet.setDuration(150);
		showSet.addListener(this);

		ObjectAnimator hideTranslationY = ObjectAnimator.ofFloat(ll_parent, "translationY", 300f);
		ObjectAnimator hideAlpha = ObjectAnimator.ofFloat(ll_parent, "alpha", 0f);
		ObjectAnimator hideAlphaC = ObjectAnimator.ofFloat(ll_container, "alpha", 0f);

		hideSet.play(hideTranslationY).with(hideAlpha).with(hideAlphaC);
		hideSet.setDuration(150);
		hideSet.addListener(this);
	}

	private boolean shouldShow;

	@Override
	public void show() {
		shouldShow = true;
		showSet.start();
	}

	@Override
	public void dismiss() {
		shouldShow = false;
		hideSet.start();
	}

	@Override
	public void onAnimationStart(Animator animation) {
		if (!isShowing() && shouldShow)
			super.show();
	}

	@Override
	public void onAnimationEnd(Animator animation) {
		if (isShowing() && !shouldShow)
			super.dismiss();
	}

	@Override
	public void onAnimationCancel(Animator animation) {
	}

	@Override
	public void onAnimationRepeat(Animator animation) {
	}

}
